package com.egzosn.pay.spring.boot.demo.config;

import com.egzosn.pay.common.bean.CertStoreType;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.spring.boot.core.PayServiceConfigurer;
import com.egzosn.pay.spring.boot.core.configurers.MerchantDetailsServiceConfigurer;
import com.egzosn.pay.spring.boot.core.merchant.bean.UnionMerchantDetails;

import com.egzosn.pay.spring.boot.core.configurers.PayMessageConfigurer;
import com.egzosn.pay.spring.boot.core.merchant.PaymentPlatform;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.AliPaymentPlatform;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.PaymentPlatforms;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.WxPaymentPlatform;
import com.egzosn.pay.spring.boot.demo.config.handlers.AliPayMessageHandler;
import com.egzosn.pay.spring.boot.demo.config.handlers.WxPayMessageHandler;
import com.egzosn.pay.spring.boot.demo.config.interceptor.AliPayMessageInterceptor;
import com.egzosn.pay.spring.boot.core.configurers.PayMessageConfigurer;
import com.egzosn.pay.spring.boot.core.merchant.PaymentPlatform;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.AliPaymentPlatform;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.PaymentPlatforms;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.WxPaymentPlatform;
import com.egzosn.pay.spring.boot.demo.config.handlers.WxPayMessageHandler;
import com.egzosn.pay.spring.boot.demo.config.interceptor.AliPayMessageInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 支付服务配置
 *
 * @author egan
 *         email egzosn@gmail.com
 *         date 2019/5/26.19:25
 */
@Configuration
public class MerchantPayServiceConfigurer implements PayServiceConfigurer {

//    @Autowired
//    private JdbcTemplate jdbcTemplate;
    @Autowired
    private AutowireCapableBeanFactory spring;
    @Autowired
    private AliPayMessageHandler aliPayMessageHandler;
    /**
     * 商户配置
     *
     * @param merchants 商户配置
     */
    @Override
    public void configure(MerchantDetailsServiceConfigurer merchants)  {
//        数据库文件存放 /doc/sql目录下
//        merchants.jdbc().template(jdbcTemplate);
        //微信请求配置，详情参考https://gitee.com/egzosn/pay-java-parent项目中的使用
        HttpConfigStorage wxHttpConfigStorage = new HttpConfigStorage();
        wxHttpConfigStorage.setKeystore("http://www.egzosn.com/certs/ssl 退款证书");
        wxHttpConfigStorage.setCertStoreType(CertStoreType.URL);
        wxHttpConfigStorage.setStorePassword("ssl 证书对应的密码， 默认为商户号");
        //内存Builder方式
        merchants.inMemory()
                .ali()
                .detailsId("1")
                .appid("2017080308018468")
                //.keyPrivate("MIIEpAIBAAKCAQEAqk3l3za5H0N3xtKiZkMzwER/xIUZwrxuxRkoHwJlYhqToCG+0V1U/O4kCXeER5lwCUAGdK4ZAKxetaWNaWcoue0ywwAaeKN526cQ7aJ+J8uTfI2ZIqXleLQTGV5DZOhg/LcrnHtzyALCcpEVfzB4j9OeOhoTHOwZ49rLEi1cv3T19J+c9POVHBnZHB9cnAhljyKdcUmbgjelkFA8+Pgm0sH40W7Ms8DleqKX53v/p4aAbfcaxGQQfknZGECz95u9ekWCBCJi15pf4MbkCBODyYjAXaOurYqau+Q8UGCj9g7UiiC18axOmVEF38goqmsnBi5RULccaDxdFfDfGJrRjQIDAQABAoIBAQCEcVY+1Fj9lkOVDQQdEVAyy1Kt63EQP+jJQXoxO3TK6a47D2NNgQ7yMOU3s4ndANffabpbjXWGT4bMLzadbQuAJJagjfkILuI6z19b303K9wf5Ckm4Z1nhF/HufZ9d31Zl0rGI3X06Y+MCpBAavQBVohZgSxzoFjlAOUxKE5sYjMB25CbMABT2k2wNNjyEOxY8wRVY2kU3hg1T/FXM90jy/+ilUpzsNj+fZQLooxet/MiP3XmIWpBeQBOaRXsukB9oYjnLaIv7/ribWTI6O8c1oMBGEk+K9ycBxIYLUMln/JAEFgNKufZd+U03IO4lUKjYPs4nMH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AwpSTx2Kj0G0L7eYg/5PTqhnkwfvH1U2cpAClAs4Lex6zUrUBKgtS2DAlA1HDbAYqLze80WLGZr9vNvx5eYjrb2yTCgCjESs6HISkPNI8gNVlxgVWA9ndVUzRkBjD7sriwyITIKfUpg+e1VQ0liDgeASb/RpO3Wt2XJsTrf/cDnwKBgE9JP98VCJ3xUhwsgQS+VRP4+t88Yiu1of1Um4WXkygGqHfyLwKc7fR88c//u5PhZdOhYox1Ak+F3ezmpvN/bBZhQGKZhAJKSeug7NmKyGPh69cGn5O2IeSM1SO/wNpfuwCc5OeMfD82vXISy6dmIiids0a89azWE2yVHrSG6ZlO")
                .keyPublic("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr5xBK5cMaGdwwUTUkCGjMFbJ4GV2CUk7HvNXiWU0SSADS0dak49u6MJ5SAJIQfwm4ixSGOfxiW6J0dU5ld26rxF4SQbLJvJfJuyq98ykRvdb5XuuveIagippdK5L9GUFdgS3fDfyoFuf/+JgBo7MPctcj0DwJij8FvK4nRYHkMa7JDafKpxrXCmSwkD2kjeNrO8o2ZPaLIQ4ZA5O3S/+N4HjQ/dnpe9mJNbeXFh/YB3M+FoLL8l3vSOTqugkOh3s1ijBfRcJKlH7y0R0Q9R7OljLVzU+m2NbBfdJrtWpPUoLEjghmWIGUDHp2DGujUL0SBhSwYh1qVz62uBjdp86XwIDAQAB/+JgBo7MPctcj0DwJij8FvK4nRYHkMa7JDafKpxrXCmSwkD2kjeNrO8o2ZPaLIQ4ZA5O3S/+N4HjQ/dnpe9mJNbeXFh/YB3M+FoLL8l3vSOTqugkOh3s1ijBfRcJKlH7y0R0Q9R7OljLVzU+m2NbBfdJrtWpPUoLEjghmWIGUDHp2DGujUL0SBhSwYh1qVz62uBjdp86XwIDAQAB")//支付宝公钥
                .inputCharset("utf-8")
                .notifyUrl("http://113.116.35.210:5500/pay/payBack1.json")//返回结果异步处理
                .returnUrl("http://113.116.35.210:5500/pay/payBack1.json")//返回结果同步处理
                .pid("2088721640672823")
                .seller("2088721640672823")
                .signType("RSA2")
                .test(false)
                .and()
                .wx()
                .detailsId("2")
                .appid("wx3344f4aed352deae")
                .mchId("1469188802")
                .secretKey("991ded080***************f7fc61095")
                .notifyUrl("http://pay.egzosn.com/payBack2.json")
                .returnUrl("http://pay.egzosn.com/payBack2.json")
                .inputCharset("utf-8")
                .signType("MD5")
                //设置请求相关的配置
                .httpConfigStorage(wxHttpConfigStorage)
                .test(true)
                .and()
        ;

      /*  //------------内存手动方式------------------
        UnionMerchantDetails unionMerchantDetails = new UnionMerchantDetails();
        unionMerchantDetails.detailsId("3");
        //内存方式的时候这个必须设置
        unionMerchantDetails.setCertSign(true);
        unionMerchantDetails.setMerId("700000000000001");
        //公钥，验签证书链格式： 中级证书路径;
        unionMerchantDetails.setAcpMiddleCert("D:/certs/acp_test_middle.cer");
        //公钥，根证书路径
        unionMerchantDetails.setAcpRootCert("D:/certs/acp_test_root.cer");
        //私钥, 私钥证书格式： 私钥证书路径
        unionMerchantDetails.setKeyPrivateCert("D:/certs/acp_test_sign.pfx");
        //私钥证书对应的密码
        unionMerchantDetails.setKeyPrivateCertPwd("000000");
        //证书的存储方式
        unionMerchantDetails.setCertStoreType(CertStoreType.PATH);
        unionMerchantDetails.setNotifyUrl("http://127.0.0.1/payBack4.json");
        // 无需同步回调可不填  app填这个就可以
        unionMerchantDetails.setReturnUrl("http://127.0.0.1/payBack4.json");
        unionMerchantDetails.setInputCharset("UTF-8");
        unionMerchantDetails.setSignType("RSA2");
        unionMerchantDetails.setTest(true);
        //手动加入商户容器中
        merchants.inMemory().addMerchantDetails(unionMerchantDetails);*/
    }
    /**
     * 商户配置
     *
     * @param configurer 支付消息配置
     */
    @Override
    public void configure(PayMessageConfigurer configurer) {
        PaymentPlatform aliPaymentPlatform = PaymentPlatforms.getPaymentPlatform(AliPaymentPlatform.platformName);
        configurer.addHandler(aliPaymentPlatform, aliPayMessageHandler);
        configurer.addInterceptor(aliPaymentPlatform, spring.getBean(AliPayMessageInterceptor.class));
        configurer.addHandler(PaymentPlatforms.getPaymentPlatform(WxPaymentPlatform.platformName), new WxPayMessageHandler());
    }
}
